<template>
  <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">

    <div class="title-container">
      <h3 class="title">黑马智慧城市后台管理系统</h3>
    </div>

    <el-form-item prop="username">
      <span class="svg-container"><svg-icon icon-class="user"/></span>
      <el-input ref="username" v-model="loginForm.username" placeholder="请输入密码"/>
    </el-form-item>

    <el-form-item prop="password">
      <span class="svg-container"><svg-icon icon-class="password"/></span>
      <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" @keyup.enter.native="handleLogin"/>
      <span class="show-pwd" @click="changePasswordType">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
    </el-form-item>

    <el-button type="primary" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>

    <div class="tips">
      <span> 仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！ </span>
    </div>

  </el-form>
</template>

<script>


export default {
  name: 'Login',
  data() {
    return {
      /* 表单数据对象 */
      loginForm: { username: 'demo', password: 'zh@hm#23' },
      /* 表单规则对象 */
      loginRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      /* 按钮的loading状态 */
      loading: false,
      /* 密码框的类型 */
      passwordType: 'password',
      /* 回跳地址 */
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {

    /* 点击密码框眼睛的回调函数 */
    changePasswordType() {
      this.passwordType = this.passwordType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    /* 登录的回调函数 */
    async doLogin() {
      try {
        this.loading = true
        const loginResult = await this.$store.dispatch('user/userLogin', this.loginForm)
        this.$message.success(loginResult)
        this.$router.push(this.redirect || '/')
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },

    /* 点击登录按钮的回调函数 */
    async handleLogin() {
      const valida = await this.$refs.loginForm.validate().catch(error => error)
      if (!valida) return
      await this.doLogin()
    }
  }
}
</script>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-form {
  position: relative;
  width: 520px;
  max-width: 100%;
  padding: 160px 35px 0;
  margin: 0 auto;
  overflow: hidden;
}

.tips {
  font-size: 14px;
  color: #ff0000;
  margin-bottom: 10px;

  span {
    &:first-of-type {
      margin-right: 16px;
    }
  }
}

.svg-container {
  padding: 6px 5px 6px 15px;
  color: $dark_gray;
  vertical-align: middle;
  width: 30px;
  display: inline-block;
}

.title-container {
  position: relative;

  .title {
    font-size: 30px;
    color: #4770ff;
    margin: 0 auto 50px auto;
    text-align: center;
    font-weight: bold;
  }
}

.el-button {
  width: 100%;
  height: 50px;
  margin-bottom: 30px;
  font-size: 15px;
}

.show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
}

</style>
